package com.supor.aiot.mine.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.android.baseconfig.base.BaseSharedPreferences;
import com.android.baseconfig.base.RxBus3;
import com.android.baseconfig.common.config.HttpConstant;
import com.android.baseconfig.common.helper.ActivityManager;
import com.android.baseconfig.common.utils.ToastUtils;
import com.android.baseconfig.common.views.AppBasicDialog;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseMVVMActivity;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.common.helper.WsHelper;
import com.supor.aiot.common.push.PushMsgService;
import com.supor.aiot.common.ui.activity.WebToPDFDownloadActivity;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.databinding.ActivityPrivacySafeServiceBinding;

import io.reactivex.rxjava3.functions.Consumer;

public class PrivacySafeServiceActivity extends BaseMVVMActivity<SettingViewModel, ActivityPrivacySafeServiceBinding> implements View.OnClickListener {

    private static final int TYPE = 1;
    private static final String HOST = "https://i.supor.com/";
    private AppBasicDialog deleteDialog;


    @Override
    protected int resourceId() {
        return R.layout.activity_privacy_safe_service;
    }

    @Override
    protected void initView() {
        super.initView();
        bindingView.setViewM(this);
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
        bindingView.toolbar.setTvLeftOnClickListener(v -> finish());

        bindingView.rlPrivacy.setOnClickListener(this);
        bindingView.rlService.setOnClickListener(this);
        bindingView.rlOneKeyLogin.setOnClickListener(this);
        bindingView.rlSdkInfo.setOnClickListener(this);
        bindingView.cvLogoff.setOnClickListener(v ->
                showDeleteDialog(getString(R.string.delete_user_title), getString(R.string.delete_user_confirm)));

        if (!AppAgent.isLogin()) bindingView.llBtn.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        super.initData();
        viewModel.getDeleteResult().observe(this, result -> {
            closeLoading();
            if (result instanceof Result.Success) {
                ToastUtils.getInstance().showCustomToast("注销App用户成功");
                PushMsgService.unbindUser();
                BaseSharedPreferences.setString(AppAgent.getApp(), Constants.Key.USER_INFO, "");
                BaseSharedPreferences.setString(this, HttpConstant.TOKEN, "");
                GlobalDataCache.userInfo = null;
                GlobalDataCache.deviceTree = null;
                AppAgent.setLogin(false);
                WsHelper.getInstance().exit();
                RxBus3.getInstance().post(Constants.Tag.LOGOUT, Constants.Value.SUCCESS);

                bindingView.llBtn.setVisibility(View.GONE);
                ActivityManager.finishAll("MainActivity");
            } else if (result instanceof Result.Error) {
                ToastUtils.getInstance().showCustomToast("注销App用户失败: " + ((Result.Error) result).getErrorMsg());
            }
        });

        RxBus3.getInstance().register(Constants.Tag.LOGOUT, (Consumer<String>) s -> {
            if (Constants.Value.SUCCESS.equalsIgnoreCase(s)) {
                if (bindingView.llBtn.getVisibility() != View.GONE) {
                    bindingView.llBtn.setVisibility(View.GONE);
                }
            }
        }, this.getClass());
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.rl_one_key_login) {
            startActivity(new Intent(this, OneKeyLoginActivity.class));
            return;
        }
        String title = getString(R.string.sdk_info), url = Constants.APP_PROTOCOL_BASE + "SDK_LIST";
        if (id == R.id.rl_privacy) {
            title = getString(R.string.privacy);
            url = Constants.USERPRIVACYAGREE;
        } else if (id == R.id.rl_service) {
            title = getString(R.string.service);
            url = Constants.USERSERVERAGREE;
        }
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.Key.param_type, TYPE);
        bundle.putString(Constants.Key.param_url, HOST + url);
        bundle.putString(Constants.Key.param_title, title);
        intent.putExtras(bundle);
        intent.setClass(this, WebToPDFDownloadActivity.class);
        startActivity(intent);
    }

    private void showDeleteDialog(String title, String message) {
        if (deleteDialog != null && deleteDialog.isShowing()) {
            deleteDialog.dismiss();
        }
        AppBasicDialog.Builder builder = new AppBasicDialog.Builder(this, false);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(R.string.sure, (dialog, which) -> {
            showLoading();
            viewModel.deleteUser(GlobalDataCache.userInfo.getBossUserId());
            dialog.cancel();
        });
        builder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.cancel());
        deleteDialog = builder.create();
        deleteDialog.setCancelable(false);
        deleteDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus3.getInstance().unregister(this.getClass());
    }
}